class Solution(object):
    def wordPattern(self, pattern, s):
        """
        :type pattern: str
        :type s: str
        :rtype: bool
        """
        s = s.split()
        if len(s) != len(pattern):
            return False
        n = len(s)
        d1 = {}
        d2 = {}
        for i in range(n):
            if pattern[i] not in d1:
                if s[i] not in d2:
                    d1[pattern[i]] = s[i]
                    d2[s[i]] = pattern[i]
                else:
                    return False
            else:
                if s[i] not in d2:
                    return False
                else:
                    if d1[pattern[i]] != s[i]:
                        return False
        return True

data = Solution()
pattern = "abba"
s = "dog cat cat dog"
print(data.wordPattern(pattern, s))